package com.java110.community.cmd.ownerRepair;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.community.cmd.community.UpdateCommunityCmd;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.core.log.LoggerFactory;
import com.java110.dto.repair.RepairDto;
import com.java110.dto.repair.RepairPhotoDto;
import com.java110.dto.repair.RepairStaffsFlow;
import com.java110.dto.repair.RepairStateEnum;
import com.java110.intf.community.IRepairInnerServiceSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.DateUtil;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Java110Cmd(serviceCode = "ownerRepair.repairFinish")
public class RepairFinishCmd extends Cmd {
    private static Logger logger = LoggerFactory.getLogger(UpdateCommunityCmd.class);
    @Autowired
    private IRepairInnerServiceSMO repairInnerService;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException, ParseException {

    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException, ParseException {
        logger.info("保存保修配置请求入参：{}", reqJson.toJSONString());
        // 获取报修后照片
        JSONArray afterRepairPhotos = reqJson.getJSONArray("afterRepairPhotos");
        Date date = DateUtil.getCurrentDate();
        List<RepairPhotoDto> afterPhotoMap = afterRepairPhotos.stream().map(s -> {
            RepairPhotoDto dto = new RepairPhotoDto();
            dto.setRepairId(reqJson.getString("repairId"));
            dto.setCommunityId(reqJson.getString("communityId"));
            dto.setPhoto(JSONObject.parseObject(JSONObject.toJSONString(s)).getString("photo"));
            dto.setForwardOrRear("1");
            dto.setUserName(reqJson.getString("userName"));
            dto.setUserId(reqJson.getString("userId"));
            dto.setCreatTime(date);
            return dto;
        }).collect(Collectors.toList());
        // 获取报修前照片
        JSONArray beforeRepairPhotos = reqJson.getJSONArray("beforeRepairPhotos");
        List<RepairPhotoDto> beforePhotoMap = beforeRepairPhotos.stream().map(s -> {
                    RepairPhotoDto dto = new RepairPhotoDto();
                    dto.setRepairId(reqJson.getString("repairId"));
                    dto.setCommunityId(reqJson.getString("communityId"));
                    dto.setPhoto(JSONObject.parseObject(JSONObject.toJSONString(s)).getString("photo"));
                    dto.setForwardOrRear("0");
                    dto.setUserName(reqJson.getString("userName"));
                    dto.setUserId(reqJson.getString("userId"));
                    dto.setCreatTime(date);
                    return dto;
                }
        ).collect(Collectors.toList());
        beforePhotoMap.addAll(afterPhotoMap);
        repairInnerService.repairFinish(beforePhotoMap);
        //有没有使用商品
        JSONArray choosedGoodsList = reqJson.getJSONArray("choosedGoodsList");
        if (choosedGoodsList != null && choosedGoodsList.size() > 0) {
            List<HashMap<Object, Object>> choosedList = choosedGoodsList.stream().map(s -> {
                HashMap<Object, Object> map = new HashMap<>();
                JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(s));
                map.put("useNumber", jsonObject.getString("useNumber"));
                map.put("rstName", jsonObject.getString("rstName"));
                map.put("price", jsonObject.getString("price"));
                map.put("isCustom", jsonObject.getBoolean("isCustom"));
                map.put("customGoodsName", jsonObject.getString("customGoodsName"));
                map.put("communityId", reqJson.getString("communityId"));
                map.put("repairId", reqJson.getString("repairId"));
                map.put("userName", reqJson.getString("userName"));
                map.put("userId", reqJson.getString("userId"));
                map.put("creatTime", date);
                return map;
            }).collect(Collectors.toList());
            repairInnerService.choosedGoodsList(choosedList);
        }
        //修改保修单状态
        RepairDto repairDto = repairInnerService.getOneById(reqJson.getLong("repairId"));
        repairDto.setState(RepairStateEnum.RETURN_VISIT.getState());
        repairDto.setStateName(RepairStateEnum.RETURN_VISIT.getStateName());
        repairDto.setUpdateTime(date);
        JSONObject jsonObject = BeanConvertUtil.beanCovertJson(repairDto);
        jsonObject.put("repairType", repairDto.getId());
        repairInnerService.repairDispatch(jsonObject);
        //修改流转单状态
        RepairStaffsFlow repairStaffsFlow = repairInnerService.lastTimeRepairFlow(repairDto.getId());
        repairStaffsFlow.setEndTime(DateUtil.getCurrentDate());
        repairStaffsFlow.setTimeConsuming(System.currentTimeMillis() - repairStaffsFlow.getCreatTime().getTime());
        repairInnerService.updateRepairDispatchFlow(repairStaffsFlow);
        //新增流转单
        //添加当前流转单
        repairStaffsFlow.setId(null);
        repairStaffsFlow.setEndTime(null);
        repairStaffsFlow.setCreatTime(DateUtil.getCurrentDate());
        repairStaffsFlow.setTimeConsuming(null);
        repairStaffsFlow.setState(Integer.parseInt(RepairStateEnum.RETURN_VISIT.getState()));
        repairStaffsFlow.setStateName(RepairStateEnum.RETURN_VISIT.getStateName());
        repairStaffsFlow.setContext(reqJson.getString("context"));
        JSONObject beanedCovertJson = BeanConvertUtil.beanCovertJson(repairStaffsFlow);
        beanedCovertJson.put("createUserName", reqJson.get("userName"));
        beanedCovertJson.put("currentUserId", reqJson.get("userId"));
        repairInnerService.repairDispatchFlow(beanedCovertJson);

    }
}
